<?php
/**
 * Created by PhpStorm.
 * User: bxm
 * Date: 2022/7/15
 * Time: 0:21
 */

namespace app\api\controller\worker;


use app\api\controller\Base;
use app\common\model\node\NodeProcessModel;
use app\common\model\worker\CustomerDrawModel;
use app\common\model\worker\CustomerGoodsDataModel;
use app\common\model\worker\CustomerRulerDetailModel;
use app\common\model\worker\TaskModel;
use think\facade\Db;
use think\Request;

class CustomerDrawController  extends Base
{
    private $model;
    private $taskModel;
    /**
     * 构造方法
     * @access public
     * @throws \Exception
     */
    public function __construct()
    {

        parent::__construct();
        $this->checkLogin();
        $this->model = new CustomerDrawModel();
        $this->taskModel = new TaskModel();

    }
    //完成出图
    public function finishPicEdit(Request $request){
//        $roles_ids = $this->role_ids;
//        if(!in_array(8,$roles_ids)){
//
//            $this->result([],'该用户没有权限，只有客户代表有操作权限！',0);
//        }
        $param = $request->param();
        //开始校验
        $this->validate($param,[
            'customer_id|客户id'=>'require',
            'goods_name|商品名称'=>'require',
            'goods_data_id|商品id'=>'require',

        ]);
        $info = CustomerGoodsDataModel::where('id',$param['goods_data_id'])->find();
//        if($info['budget_status'] != 1 || $info['design_status'] != 1){
//
//            $this->result([],'请先完成预算或已完量房后，再操作！！',0);
//        }
        if($info['draw_id']){

            $this->result([],'已完成出图，请勿重复操作！！',0);
        }
        $data['goods_name'] = $param['goods_name'];
        $data['goods_data_id'] = $param['goods_data_id'];
        $data['customer_id'] = $param['customer_id'];
        //$data['drawing_remark'] = $param['drawing_remark'];
        $data['drawing_imgs'] = json_encode($param['detail_imgs'],JSON_UNESCAPED_UNICODE);
        $data['action_admin_id'] =$this->loginInfo['id'];
        Db::startTrans();
        $customerDrawId = $this->model->insertGetId($data);
        if(!$customerDrawId){
            //echo 11;
            // 回滚事务
            Db::rollback();
            $this->result([], "操作失败", 0);
        }

        $resutl = CustomerGoodsDataModel::where('id',$param['goods_data_id'])->update(['draw_id'=>$customerDrawId, 'design_status'=>2]);
        if(!$resutl){
            //echo 2;
            // 回滚事务
            Db::rollback();
            $this->result([], "操作失败", 0);
        }
        $nodeProcess = NodeProcessModel::where('node_id', 8)->order('id', 'asc')->find();

        $dataTask = [
            'customer_id' => $data['customer_id'],
            'order_id' => 0,
            'node_id' => 8,
            'process_id' => $nodeProcess['id'],
            'receive_admin_id' => $nodeProcess['audit_admin_id'],
            'action_table_id'=>$customerDrawId,//操作表id
            'admin_id'=>$this->loginInfo['id']
        ];
        $result = $this->taskModel->commonAddTask($dataTask);
        if(!$result){
            //echo 33;
            // 回滚事务
            Db::rollback();
            $this->result([], "操作失败", 0);
        }
        Db::commit();
        $this->taskModel->sendTaskInfo($dataTask['receive_admin_id'],$nodeProcess['process_name']);
        $this->result($result,'操作成功',1);

    }

    public function list(Request $request){
        $param = $request->param();
        $pageSize      = $this->request->param('pageSize/d', 20); //每页多少条
        $pageNumber    = $this->request->param('pageNumber/d') ?? 1; //当前页
        //开始校验
        $this->validate($param,[
            'customer_id|客户id'=>'require',

        ]);
        $list = $this->model->alias('cal')
            ->leftJoin('admin a', 'a.id = cal.action_admin_id')
            ->where('cal.customer_id',$param['customer_id'])
            ->order('cal.id','desc')
            ->field('a.nickname,cal.*')
            ->paginate($pageSize, false, ['page' =>$pageNumber ]);

        $this->result($list,'操作成功',1);

    }
    public function getInfo(Request $request)
    {
        $param = $request->param();
        //开始校验
        $this->validate($param, [
            'id|记录id' => 'require',
        ]);
        $id = $param['id'];
        $list = $this->model->alias('cal')->leftJoin('admin a', 'a.id = cal.action_admin_id')
            ->where('cal.id', $id)
            ->field('a.nickname,cal.*')
            ->find();
        $this->result($list, '操作成功', 1);
    }
}